$(function () {
  var editFlag = permissions.pre_code_edit
  var delFlag = permissions.pre_code_del
  var addFlag = permissions.pre_code_add
  if (!addFlag) {
    $('#addBtn').remove()
  }
  getInsystem()

  $('#sysCode').change(function(){
    var val = $(this).val()
    if (val) getGroup(val)
  })

  var options = {
    url: api + "/comm/syscommpkt/pktHedPage",
    updateUrl: "/pages/pre/code/edit",
    removeUrl: api + "/comm/syscommpkt/{id}",
    modalName: "交易码配置",
    columns: [
      {
        title: "序号",
        formatter: function (value, row, index) {
          return $.table.serialNumber(index);
        }
      },
      {
        field: 'inSysName',
        title: '项目名称'
      },
      {
        field: 'systemCode',
        title: '项目编码'
      },
      {
        field: 'groupName',
        title: '服务名称'
      },
      {
        field: 'groupCode',
        title: '服务编码'
      },
      {
        field: 'transCode',
        title: '交易码'
      },
      {
        title: '操作',
        formatter: function (value, row, index) {
          var actions = [];
          if (editFlag) {
            var url = "/pages/pre/code/edit?systemCode="+row.systemCode+"&groupCode="+(row.groupCode?row.groupCode:'')+"&oper=edit"
            actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="$.operate.editFullBpm(\'' + row.pktId + '\',\'\',\'' + url + '\')"><i class="fa fa-edit"></i>编辑</a> ');
          }
          if (delFlag) {
            actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.pktId + '\')"><i class="fa fa-remove"></i>删除</a> ');
          }
          return actions.join('');
        }
      }]
  };
  $.table.init(options);
});


//获取接入项目列表
function getInsystem(){
  $.operate.get(api + '/comm/insystem/insys_list', function (res) {
    res.data.forEach(function (item) {
      $('#sysCode').append('<option value="' + item.code + '">' + item.name + '</option>')
    })
  }, false)
}

//根据接入项目名称获取分组列表
function getGroup(code){
  $('#groupCode').find('option').remove()
  $('#groupCode').append('<option value=""></option>')
  $.operate.get(api + '/comm/group/group_list/code/'+code, function (res) {
    res.data.forEach(function (item) {
      $('#groupCode').append('<option value="' + item.code + '">' + item.name + '</option>')
    })
  }, false)
}
